package com.microstation.controller.system;

import com.microstation.common.AppConst;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.entity.po.QuestionnaireRec;
import com.microstation.entity.po.UserInfo;
import com.microstation.entity.po.UserSigninInfo;
import com.microstation.entity.po.view.UserInfoSigninView;
import com.microstation.entity.po.view.UserQuesRecView;
import com.microstation.entity.vo.UserInfoVO;
import com.microstation.enums.ResultEnum;
import com.microstation.service.QuestionnaireRecService;
import com.microstation.service.UserInfoService;
import com.microstation.service.UserSigninInfoService;
import com.microstation.service.view.UserInfoSigninViewService;
import com.microstation.service.view.UserQuesRecViewService;
import com.microstation.util.SearchForm;
import com.microstation.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/userManage")
public class UserManageController extends BaseController {
    private final static Logger LOG = LoggerFactory.getLogger(UserManageController.class);

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoSigninViewService userInfoSigninViewService;
    @Autowired
    private UserSigninInfoService userSigninInfoService;
    @Autowired
    private UserQuesRecViewService userQuesRecViewService;

    @PostMapping(value = "/loadList")
    public Result loadList(SearchForm searchForm) {
        try {
            UserInfoSigninView userInfoSigninView = new UserInfoSigninView();
            Assist assist = new Assist();
            assist.setRowSize(searchForm.getLimit());
            assist.setPage(searchForm.getPageIndex() + 1);
            if (StringUtil.isNotEmpty(searchForm.getKey())) {
                userInfoSigninView.setKey(searchForm.getKey());
            }
            assist.setOrder(Assist.order("id", false));
            Result result = userInfoSigninViewService.find(userInfoSigninView, assist);
            return new Result(result.getResults(), result.getRows());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getInfo(), 0L);
        }
    }

    /**
     * 手工签到
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/updateUserSignin")
    public Result updateUserSignin(String ids, long actId) {
        try {
            return userSigninInfoService.updateUserSignin(ids, actId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), e.getMessage(), AppConst.NULL);
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/removeUserInfo")
    public Result removeUserInfo(String ids) {
        try {
            return userSigninInfoService.removeUserInfo(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), e.getMessage(), AppConst.NULL);
        }
    }

    /**
     * 用户问卷列表
     *
     * @param userQuesRecView
     * @param searchForm
     * @return
     */
    @PostMapping(value = "/loadUserQuesList")
    public Result loadUserQuesList(UserQuesRecView userQuesRecView, SearchForm searchForm) {
        try {
            Assist assist = new Assist();
            assist.setRowSize(searchForm.getLimit());
            assist.setPage(searchForm.getPageIndex() + 1);
            assist.setOrder(Assist.order("seq", true));
            Result result = userQuesRecViewService.find(userQuesRecView, assist);
            return new Result(result.getResults(), result.getRows());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getInfo(), 0L);
        }
    }

    @RequestMapping(value = "/signinPrint")
    public Result signinPrint(String qrcode) {
        try {
            if (StringUtil.isEmpty(qrcode)) {
                return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), "二维码为空", AppConst.NULL);
            }
            Result result = new Result();
            Map<String, Object> map = new HashMap<String, Object>();
            UserInfoSigninView queryParam = new UserInfoSigninView();
            queryParam.setQrcode(qrcode);
            queryParam.setDelFlag(1);
            queryParam.setRegFlag(1);
            UserInfoSigninView userInfoSigninView = userInfoSigninViewService.getByExample(queryParam);
            if (userInfoSigninView != null) {
                if (userInfoSigninView.getSignCount().intValue() <= 0) {
                    UserSigninInfo userSigninInfo = new UserSigninInfo();
                    userSigninInfo.setUserId(userInfoSigninView.getId());
                    userSigninInfo.setActId(userInfoSigninView.getActId());
                    userSigninInfoService.saveNotNull(userSigninInfo);
                }
                String levelStr = "";
                if (userInfoSigninView.getLevel().intValue() == 1) {
                    levelStr = "普通";
                } else if (userInfoSigninView.getLevel().intValue() == 2) {
                    levelStr = "普通";
                } else if (userInfoSigninView.getLevel().intValue() == 3) {
                    levelStr = "VIP";
                } else if (userInfoSigninView.getLevel().intValue() == 4) {
                    levelStr = "VIP";
                }
                map.put("name", userInfoSigninView.getName());
                map.put("level", levelStr);
                map.put("seat", userInfoSigninView.getSeat());
                result.setHasError(Boolean.FALSE);
                result.setData(map);
                return result;
            }
            result.setHasError(Boolean.TRUE);
            result.setResultEnum(ResultEnum.EMPTY, "邀请码错误");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), e.getMessage(), AppConst.NULL);
        }
    }
}
